<div class="container-fluid">
    <!-- Page Heading -->
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header">
                QAS <small>Aturan 1</small>
            </h1>
            <ol class="breadcrumb">
                <li class="active">
                    <i class="fa fa-dashboard"></i> QAS Terjemah Al Qur'an Bahasa Indonesia
                </li>
            </ol>
        </div>
    </div>
    <!-- /.row -->
    
    <div class="row">
        <div class="my-content">
            <!-- CONTENT -->
            <form class="form-horizontal" method="post" action="">
                <div class="control-group" style="width: 100%;">
                    <label class="control-label" for="pertanyaan">Pertanyaan</label>
                    <div class="controls" style="display: inline-block; margin-left: 10px; width:80%;">
                        <input style="width: 70%; border-radius: 10px; vertical-align: middle; padding-left: 14px; padding-right: 14px; background-color: #ffffff;
                border: 1px solid #cccccc; box-shadow: 0 1px 1px rgba(0, 0, 0, 0.075) inset; transition: border 0.2s linear 0s, box-shadow 0.2s linear 0s; font-size: 14px;
                height: 35px; line-height: 20px;" type="text" id="pertanyaan" name="keyword" placeholder="Siapa penghuni neraka?" class="input-xxlarge search-query">
                        <button type="submit" name="search" class="btn" style="-moz-border-bottom-colors: none; -moz-border-left-colors: none;
                -moz-border-right-colors: none; -moz-border-top-colors: none; background-color: #f5f5f5; background-image: linear-gradient(to bottom, #ffffff, #e6e6e6);
                background-repeat: repeat-x; border-color: rgba(0, 0, 0, 0.15) rgba(0, 0, 0, 0.15) rgba(0, 0, 0, 0.25); border-image: none; border-radius: 15;
                border-style: solid; border-width: 1px; box-shadow: 0 1px 0 rgba(255, 255, 255, 0.2) inset, 0 1px 2px rgba(0, 0, 0, 0.05); color: #333333; cursor: pointer; display: inline-block; font-size: 14px; line-height: 20px; margin-bottom: 0; text-align: center; text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);
                vertical-align: middle;">Search</button>
                    </div>
                </div>
                <a id="tampilkan-score" style="font-weight: bold; font-style: italic;">Tampilkan Score</a>
                <a id="sembunyikan-score" style="font-weight: bold; font-style: italic;">Sembunyikan Score</a>
            </form>
            <br/>

            <?php 
            if (!isset($_SESSION['keyword'])) {
                $_SESSION['keyword'] = '';
            }
            $_SESSION['keyword'] = isset($_POST['keyword']) ? str_replace("'", "", $_POST['keyword']) : str_replace("'", "", $_SESSION['keyword']);
            
            if (!empty($_SESSION['keyword'])) :
            ?>
            <div class="alert alert-success" style="margin-top:10px;"><?php echo $_SESSION['keyword']; ?></div>
            <?php
            endif;

            ?>
            <br/>
            <style type="text/css">
                .table thead th {
                    vertical-align: middle;
                }
                table td.ayat-arabic {
                    font-family: myFirstFont;
                    text-align: center;
                    font-size: 25px;
                    letter-spacing: 1px;
                    vertical-align: middle;
                    weight: 10%;
                    line-height:40px;
                }
                table td.ayat {
                    text-align: center;
                    vertical-align: middle;
                    weight: 10%;
                    line-height:40px;
                }
            </style>

            <?php 

            if (isset($_POST['vote'])) {
                if (!empty($_POST['list-checkbox'])) {
                    //======
                    $sql  = "SELECT * FROM `list_jawaban` WHERE LOWER(pertanyaan) = '".strtolower($_POST['pertanyaan'])."'";
                    $listDataSebenarnya = array();
                    $dataListJawaban = null;
                    foreach ($pdo->query($sql) as $row) {
                        $dataListJawaban = $row['hasil'];
                    }
                    if ($dataListJawaban !== null) {
                        $listDataSebenarnya = explode("&", $dataListJawaban);
                    }
                    //======
                    $listCheckbox = explode("&", $_POST['list-checkbox']);
                    $dataCheckbox = array();
                    $jumlahCheck  = 0;
                    $jumlahData   = 0;
                    $jumlahDataReal = count($listDataSebenarnya);
                    $detailVote = array();
                    foreach ($listCheckbox as $data) {
                        if (in_array($data, $listDataSebenarnya)) {
                            $jumlahCheck++;
                        }
                        $jumlahData++;
                    }
                    $detailHasil = implode("&", $detailVote);

                    $precision = ($jumlahCheck/$jumlahData)*100;
                    $recall = ($jumlahCheck/$jumlahDataReal)*100;
                    if (($precision+$recall) > 0) {
                        $fmeasure = 2*(($precision*$recall))/($precision+$recall);
                    } else {
                        $fmeasure = 0;
                    }
                    // $fmeasure = 2/((1/$recall)+(1/$precision));

                    $tanggal = new \DateTime;
                    $sql = "INSERT INTO `vote_hasil` (`tanggal` ,`nama` ,`umur` ,`jenis_kelamin` ,`pertanyaan` ,`hasil` , `detail_vote`,`jumlah_relevan` ,`jumlah_data` ,`jumlah_seharusnya` ,`recall` ,`precision` ,`fmeasure`, `aturan`) VALUES (:tanggal, :nama, :umur, :jenis_kelamin, :pertanyaan, :hasil, :detail_vote, :jumlah_relevan, :jumlah_data, :jumlah_seharusnya, :recall, :precision, :fmeasure, :aturan)";
                    $q   = $pdo->prepare($sql);
                    $q->execute(
                        array(
                            ':tanggal'            => $tanggal->format('Y-m-d H:i:s'),
                            ':nama'               => strtoupper($_SESSION['username']),
                            ':umur'               => $_SESSION['umur'],
                            ':jenis_kelamin'      => strtoupper($_SESSION['jenis-kelamin']),
                            ':pertanyaan'         => strtolower($_POST['pertanyaan']),
                            ':hasil'              => $_POST['list-checkbox'],
                            ':detail_vote'        => $detailHasil,
                            ':jumlah_relevan'     => $jumlahCheck,
                            ':jumlah_data'        => $jumlahData,
                            ':jumlah_seharusnya'  => $jumlahDataReal,
                            ':recall'             => $recall,
                            ':precision'          => $precision,
                            ':fmeasure'           => $fmeasure,
                            ':aturan'             => 1
                        )
                    );
                }
                unset($_SESSION['keyword']);
            }

            $preProses      = new PreProcessing($pdo);
            $rulePertanyaan = new RulePertanyaanSatu($pdo);

            $stemmerFactory = new \Sastrawi\Stemmer\StemmerFactory();
            $stemmer        = $stemmerFactory->createStemmer();
            $lastResult     = $rulePertanyaan->qas($_SESSION['keyword']);

            if (!is_array($lastResult['data'])):
                echo $lastResult['data'];
            endif;

            if (is_array($lastResult['data'])): 
            $result = $lastResult['data'];
            $listNamaSuratIndonesia = $lastResult['namaSurat']['indonesia'];
            $listNamaSuratArabic = $lastResult['namaSurat']['arabic'];

            ?>
            <form method="post" action="">
                <button type="submit" name="vote" class="btn" style="-moz-border-bottom-colors: none; -moz-border-left-colors: none;
                -moz-border-right-colors: none; -moz-border-top-colors: none; background-color: #f5f5f5; background-image: linear-gradient(to bottom, #ffffff, #e6e6e6);
                background-repeat: repeat-x; border-color: rgba(0, 0, 0, 0.15) rgba(0, 0, 0, 0.15) rgba(0, 0, 0, 0.25); border-image: none; border-radius: 15;
                border-style: solid; border-width: 1px; box-shadow: 0 1px 0 rgba(255, 255, 255, 0.2) inset, 0 1px 2px rgba(0, 0, 0, 0.05); color: #333333; cursor: pointer; display: inline-block; font-size: 14px; line-height: 20px; margin-bottom: 0; text-align: center; text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);
                vertical-align: middle; width:100%;">HITUNG</button>
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th rowspan="3" class="head-table">No</th>
                        <th rowspan="3" class="head-table">Ayat</th>
                        <th colspan="3" class="head-table score-detail">Scoring</th>
                        <th rowspan="3" class="head-table score-detail">Total Score</th>
                    </tr>
                    <tr class="score-detail">
                        <th class="head-table score-detail" colspan="2" style="min-width:250px;">Wordmatch</th>
                        <th class="head-table score-detail" rowspan="2" style="min-width:125px;">Rule</th>
                    </tr>
                    <tr class="score-detail">
                        <th class="head-table score-detail" style="min-width:125px;">Asli</th>
                        <th class="head-table score-detail" style="min-width:125px;">Sinonim</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $listCheckbox = array();
                    $no = 1;
                    foreach ($result as $dataResult) :
                        ?>
                        <tr>
                            <td><?php echo $no; ?></td>
                            <td>
                            <?php
                                foreach ($dataResult['data'] as $jenis => $data) :
                                    if ($jenis == 'arabic'):
                                        echo '<div class="arabic" style="display: inline-block; float:right">';
                                        echo $data;
                                        echo '</div>';
                                        echo '<div class="arabic" style="display: inline-block">( '.$listNamaSuratArabic[$dataResult['sura']].' : '.angkaToArabic($dataResult['aya']).' )</div>';
                                    endif;
                                endforeach;
                                
                                echo '<br/>';
                                foreach ($dataResult['data'] as $jenis => $data) :
                                    if ($jenis == 'indonesia'):
                                        echo $data.' ( '.$listNamaSuratIndonesia[$dataResult['sura']].' : '.$dataResult['aya'].' )';
                                    endif;
                                endforeach;
                            ?>
                            </td>
                            <td class="score-detail">
                            <?php
                                foreach ($dataResult['data'] as $jenis => $data) :
                                    if ($jenis == 'wordmatch'):
                                        foreach ($data as $kata => $value) {
                                            echo $kata.' : '.$value['score'].'<br/>';
                                        }
                                    endif;
                                endforeach; //end for dataSura
                            ?>
                            </td>
                            <td class="score-detail">
                            <?php
                                foreach ($dataResult['data'] as $jenis => $data) :
                                    if ($jenis == 'wordmatch-sinonim'):
                                        foreach ($data as $kataAsli => $dataSin) {
                                            echo '<strong>'.$kataAsli.'</strong><br/>';
                                                foreach ($dataSin as $kata => $dataScore) {
                                                    echo $kata.': '.$dataScore['score'].'<br/>';
                                                }
                                            echo '<br/>';
                                        }
                                    endif;
                                endforeach; //end for dataSura
                            ?>
                            </td>
                            <td class="score-detail">
                            <?php
                                foreach ($dataResult['data'] as $jenis => $data) :
                                    if ($jenis == 'rule1' || $jenis == 'rule2' || $jenis == 'rule3' || $jenis == 'rule4'):
                                        echo '<strong>'.$jenis.'</strong><br/>';
                                        foreach ($data as $kata => $value) {
                                            echo $kata.' : '.$value['score'].'<br/>';
                                        }
                                        echo '<br/>';
                                    endif;
                                endforeach; //end for dataSura
                            ?>
                            </td>
                            <td class="score-detail">
                            <?php
                                foreach ($dataResult['data'] as $jenis => $data) :
                                    if ($jenis == 'total'):
                                        echo $data;
                                    endif;
                                endforeach;
                            ?>
                            </td>
                            <?php $listCheckbox[$dataResult['sura'].'-'.$dataResult['aya']] = $dataResult['sura'].'-'.$dataResult['aya']  ?>
                        </tr>
                        <?php
                        $no++;
                    endforeach;
                    ?>
                </tbody>
            </table>
            <input type="hidden" name="list-checkbox" value="<?php echo implode("&", $listCheckbox) ?>">
            <input type="hidden" name="pertanyaan" value="<?php echo $_SESSION['keyword'] ?>">
            <button type="submit" name="vote" class="btn" style="-moz-border-bottom-colors: none; -moz-border-left-colors: none;
                -moz-border-right-colors: none; -moz-border-top-colors: none; background-color: #f5f5f5; background-image: linear-gradient(to bottom, #ffffff, #e6e6e6);
                background-repeat: repeat-x; border-color: rgba(0, 0, 0, 0.15) rgba(0, 0, 0, 0.15) rgba(0, 0, 0, 0.25); border-image: none; border-radius: 15;
                border-style: solid; border-width: 1px; box-shadow: 0 1px 0 rgba(255, 255, 255, 0.2) inset, 0 1px 2px rgba(0, 0, 0, 0.05); color: #333333; cursor: pointer; display: inline-block; font-size: 14px; line-height: 20px; margin-bottom: 0; text-align: center; text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);
                vertical-align: middle; width:100%;">HITUNG</button>
            </form>
            <?php endif; ?>
        	<!-- CONTENT -->
        </div>
    </div>
    <!-- /.row -->
</div>
<!-- /.container-fluid -->